home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d14 / dlchk11.arc / SETERR.ASM < prev    next >
Assembly Source File  |  1991-04-28  |  9KB  |  254 lines

  1.     TITLE    seterr
  2. ;-------------------------------------------------------------------------------
  3. ; SETERR.ASM
  4. ; by Duane Paulson 03/12/91.
  5. ;
  6. ; MASM 5.1
  7. ;
  8. ; PURPOSE: Batch file enhancer.
  9. ;
  10. ; OUTPUT: DOS errorlevel set to a value between 0 - 255 inclusive.
  11. ; INPUT: Command line parameter 0 - 255 inclusive.
  12. ;-------------------------------------------------------------------------------
  13.  
  14.     DOSSEG
  15.     .MODEL    SMALL
  16.     .STACK    100h
  17.  
  18.     .DATA
  19.  
  20. CMD_TAIL    DB 128 DUP(?)        ; Command line arguments go here.
  21. ARG_LEN        DB ?            ; Original length of cmd line args.
  22. VAL_ARGS    DB 0            ; Will show length after non-numerals
  23.                     ;  are stripped.
  24.  
  25. CHAR1        DB '0'            ; three valid characters for processing.
  26. CHAR2        DB '0'
  27. CHAR3        DB '0'
  28.  
  29. ERR_LEV        DB ?            ; Errorlevel to be set.
  30.  
  31. WrongDOS    DB "Sorry, requires DOS version 2.0 or greater.",13,10
  32. LWrongDOS    DB $ - WrongDOS
  33. ErrorMsg    DB "Syntax: 'SETERR n' where range of 'n' is 0 to 255 inclusive.",13,10,10,"    DOS Errorlevel was not set. (Errorlevel = 0)",13,10
  34.  
  35. LErrorMsg    DB $ - ErrorMsg
  36. SuccessMsg    DB "DOS Errorlevel has been set to ???.",13,10 ; Locations 31,
  37.                                 ; 32, and 33.
  38. LSuccessMsg    DB $ - SuccessMsg
  39.  
  40.     .CODE
  41.     EVEN
  42.  
  43. Start:
  44. ;-------------------------------------------------------------------------------
  45. ; Initialize the registers.
  46. ;-------------------------------------------------------------------------------
  47.  
  48.     MOV    AX,@DATA              ; Addr of work areas
  49.     MOV    DS,AX                  ; Set data segment reg
  50.  
  51. ;-------------------------------------------------------------------------------
  52. ; Output a linefeed so messages will stand out a little better.
  53. ;-------------------------------------------------------------------------------
  54.  
  55.     MOV    AH,2            ; Output character to STDOUT
  56.     MOV    DL,10            ; Character to output
  57.     INT    21h             ; Call DOS
  58.  
  59. ;-------------------------------------------------------------------------------
  60. ; Check for DOS version compatiblilty.
  61. ;-------------------------------------------------------------------------------
  62.  
  63.     MOV    AX,3000h        ; Get DOS version.
  64.     INT    21h            ; Call DOS.
  65.     CMP    AL,0            ; Less than 2.0 returns 0.
  66.     JA    @F            ; Jump forward if greater than 0.
  67.     JMP    WrongOS            ; Long jump to error message.
  68. @@:                    ; Re-entry point for forward jump.
  69.  
  70. ;-------------------------------------------------------------------------------
  71. ; Set memory location of command tail and find out how many characters
  72. ;  it contains. Set loop counter.
  73. ;-------------------------------------------------------------------------------
  74.  
  75.     MOV    CL,BYTE PTR ES:[80H]      ; Length of command tail
  76.     XOR    CH,CH                  ; Clear hi-byte
  77.     MOV    ARG_LEN,CL        ; Set loop iterations.
  78.     DEC    CL            ; Decrement for Carriage Return
  79.     CMP    CL,0                  ; Any parmeter string?
  80.     JGE    @F            ; if found, jump forward.
  81.     JMP    Quit                  ;  else far jump to Quit.
  82. @@:                    ; Forward jump re-entry.
  83.  
  84. ;-------------------------------------------------------------------------------
  85. ; Point to buffer to receive command characters, and set up for
  86. ;  memory load and store routine.
  87. ;-------------------------------------------------------------------------------
  88.  
  89.     MOV    DI,OFFSET CMD_TAIL    ; Buffer to receive argument
  90.     MOV    SI,82H               ; Offset to command parm string
  91.     MOV    DX,ES
  92.     MOV    AX,DS                  ; Swap DS/ES
  93.     MOV    ES,AX
  94.     MOV    DS,DX
  95.     
  96. ;-------------------------------------------------------------------------------
  97. ; Read command tail a byte at a time.
  98. ; Determine whether or not character is a numeral.
  99. ; If it is, store it in a buffer area, and increment
  100. ;  the count of valid arguments.
  101. ;-------------------------------------------------------------------------------
  102.  
  103. Get:    LODSB                ; Byte from param string to AL
  104.     CMP    AL,'0'            ; Check if below 0 char.
  105.     JB    @F            ; If yes, jump forward.
  106.     CMP    AL,'9'            ; Check if above 9 char.
  107.     JA    @F            ; If yes, jump forward.
  108.  
  109.     STOSB                ; Store a character in the range
  110.                     ; 0-9 to CMD_TAIL.
  111.  
  112.     MOV    DX,ES            ; Restore DS/ES so we can
  113.     MOV    AX,DS            ;  increment a register properly.
  114.     MOV    ES,AX
  115.     MOV    DS,DX
  116.  
  117.     INC VAL_ARGS            ; Increment to show that a valid
  118.                     ; argument was found.
  119.  
  120.     MOV    DX,ES            ; Swap DS/ES back so the memory
  121.     MOV    AX,DS            ;  manipulation routine can continue.
  122.     MOV    ES,AX
  123.     MOV    DS,DX
  124.  
  125. @@:    LOOP    Get
  126.     
  127.     MOV    DX,ES            ; Restore DS/ES for normal processing.
  128.     MOV    AX,DS
  129.     MOV    ES,AX
  130.     MOV    DS,DX
  131.  
  132. ;-------------------------------------------------------------------------------
  133. ; Now that we have only numerals in CMD_TAIL, we can start to process them.
  134. ;-------------------------------------------------------------------------------
  135.  
  136.  
  137.     CMP    VAL_ARGS,4        ; Less than 4 numerals on cmnd line?
  138.     JL    @F            ; If yes, jump forward.
  139.     JMP    Quit            ; Obviously more than 255 then. Long jmp
  140. @@:                    ; Forward re-entry.
  141.  
  142. ;-------------------------------------------------------------------------------
  143. ; Find out if there are 1, 2, or 3 valid characters, and assign individaul
  144. ; variables accordingly.
  145. ;-------------------------------------------------------------------------------
  146.  
  147.     CMP    VAL_ARGS,3        ; Compare VAL_ARGS to 3
  148.     JB    @F            ; If less than 3, jump forward.
  149.  
  150.     MOV    AL,BYTE PTR CMD_TAIL[0] ; Get byte from buffer location.
  151.     MOV    CHAR1,AL        ; Assign first character to CHAR1
  152.     MOV    AL,BYTE PTR CMD_TAIL[1]
  153.     MOV    CHAR2,AL        ; Second character goes to CHAR2
  154.     MOV     AL,BYTE PTR CMD_TAIL[2]
  155.     MOV    CHAR3,AL        ; Third character goes to CHAR3
  156.     JMP    Convert            ; Jump to next routine.
  157. @@:
  158.     CMP    VAL_ARGS,2        ; Are there only two characters?
  159.     JB    @F            ; If no, jump forward.
  160.  
  161.     MOV    CHAR1,'0'        ; Assign place holder to CHAR1
  162.     MOV    AL,BYTE PTR CMD_TAIL[0]
  163.     MOV    CHAR2,AL        ; First character goes to CHAR2
  164.     MOV    AL,BYTE PTR CMD_TAIL[1]
  165.     MOV    CHAR3,AL        ; Second character goes to CHAR3
  166.     JMP    Convert
  167. @@:
  168.                     ; Only one character found.
  169.     MOV    CHAR1,'0'        ; Assign place holders to CHAR1
  170.     MOV    CHAR2,'0'        ;  and CHAR2.
  171.     MOV    AL,BYTE PTR CMD_TAIL[0]    ; Character goes to CHAR3.
  172.     MOV    CHAR3,AL
  173.  
  174. ;-------------------------------------------------------------------------------
  175. ; Convert the three characters into a number.
  176. ;-------------------------------------------------------------------------------
  177.  
  178. Convert:
  179.     MOV    AL,CHAR1        ; First char is in the hundreds place.
  180.     SUB    AL,48            ; Convert from ASCII to number.
  181.     MOV    BL,100            ; Multiply by 100
  182.     MUL    BL
  183.     JC    Quit            ; Quit if carry to AH. (Product > 255.)
  184.     MOV    ERR_LEV,AL        ; Else store the result.
  185.                     ;  Note to self: rtfm.
  186.  
  187.     MOV    AL,CHAR2        ; Tens place.
  188.     SUB    AL,48            ; ASCII to number.
  189.     MOV    BL,10            ; Multiply by 10
  190.     MUL    BL
  191.     JC    Quit            ; Overflow is still possible.
  192.     ADD    ERR_LEV,AL        ; Else add it.
  193.  
  194.     MOV    AL,CHAR3        ; Ones place.
  195.     SUB    AL,48            ; Make it a number.
  196.     ADD    ERR_LEV,AL        ; Add it.
  197.     JC    Quit            ; 256 is still greater than 255.
  198.  
  199. ;-------------------------------------------------------------------------------
  200. ; Drop the characters into SuccessMsg locations 31, 32, and 33.
  201. ;-------------------------------------------------------------------------------
  202.  
  203.     MOV AL,CHAR1
  204.     MOV BYTE PTR SuccessMsg[31],AL    ; Point at buffer location.
  205.     MOV AL,CHAR2
  206.     MOV BYTE PTR SuccessMsg[32],AL
  207.     MOV AL,CHAR3
  208.     MOV BYTE PTR SuccessMsg[33],AL
  209.  
  210.     MOV     AX,4000h        ; Output string to device.
  211.     MOV     BX,1            ; Specify STDOUT.
  212.     MOV     CL,LSuccessMsg        ; Length of string.
  213.     XOR    CH,CH            ; Turn off high byte.
  214.     MOV     DX,OFFSET SuccessMsg    ; Point to address of string.
  215.     INT     21h            ; Call DOS
  216.  
  217. ;-------------------------------------------------------------------------------
  218. ; Exit routine.
  219. ;-------------------------------------------------------------------------------
  220.  
  221. Whoa:
  222.     MOV     AH,4Ch            ; Exit with return code (errorlevel)
  223.     MOV    AL,ERR_LEV
  224.     INT     21h            ; Call DOS and exit.
  225.  
  226. ;-------------------------------------------------------------------------------
  227. ; Error routine for bad parameters or no parameters.
  228. ; Displays proper syntax and exits.
  229. ;-------------------------------------------------------------------------------
  230.  
  231. Quit:
  232.     MOV AX,4000h            ; Output string to device.
  233.     MOV    BX,1            ; Specify STDOUT.
  234.     MOV     CL,LErrorMsg        ; Length of string.
  235.     XOR    CH,CH            ; Turn off high byte.
  236.     MOV    DX,OFFSET ErrorMsg    ; Pointer to address of string
  237.     INT    21h            ; Call DOS.
  238.     JMP    Whoa            ; Bail out.
  239.  
  240. ;-------------------------------------------------------------------------------
  241. ; Error routine for DOS version 1.
  242. ;-------------------------------------------------------------------------------
  243.  
  244. WrongOS:
  245.     MOV AX,4000h            ; Output string to device.
  246.     MOV    BX,1            ; Specify STDOUT.
  247.     MOV     CL,LWrongDOS        ; Length of string.
  248.     XOR    CH,CH            ; Turn off high byte.
  249.     MOV    DX,OFFSET WrongDOS    ; Pointer to address of string
  250.     INT    21h            ; Call DOS.
  251.     JMP    Whoa            ; Bail out.
  252.  
  253.     END     Start
  254.